"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.memberRouter = void 0;
const express_1 = __importDefault(require("express"));
const MemberEntity_1 = require("../entity/MemberEntity");
const log4js_1 = require("log4js");
const typeorm_1 = require("typeorm");
const member_healper_1 = require("./member-healper");
function memberRouter(app) {
    const router = express_1.default.Router();
    const logger = (0, log4js_1.getLogger)('route/member');
    router.post('/create', async (req, res) => {
        logger.debug('Create room: ' + JSON.stringify(req.body, null, 2));
        let dbUser;
        try {
            if (Array.isArray(req.body)) {
                dbUser = [];
                for (const member of req.body) {
                    const data = await (0, member_healper_1.createMember)(member);
                    dbUser.push(data);
                }
            }
            else {
                dbUser = await (0, member_healper_1.createMember)(req.body);
            }
        }
        catch (error) {
            res.status(200).send({ result: 'Error', message: error.message });
        }
        if (dbUser) {
            res.status(200).send({ result: 'OK', data: dbUser });
        }
        else {
            res.status(200).send({ result: 'Error', message: 'user already Created!' });
        }
    });
    router.post('/update', async (req, res) => {
        let dbUser;
        logger.debug('Create room: ' + JSON.stringify(req.body, null, 2));
        try {
            if (Array.isArray(req.body)) {
                dbUser = [];
                for (const member of req.body) {
                    const data = await (0, member_healper_1.updateMember)(member);
                    dbUser.push(data);
                }
            }
            else {
                dbUser = await (0, member_healper_1.updateMember)(req.body);
            }
        }
        catch (error) {
            res.status(200).send({ result: 'Error', message: error.message });
        }
        if (dbUser) {
            res.status(200).send({ result: 'OK', data: dbUser });
        }
        else {
            res.status(1002).send({ result: 'Error', message: 'user are not fined!' });
        }
    });
    router.delete('/remove', async (req, res) => {
        const { userId } = req.body;
        logger.debug('Create room: ' + JSON.stringify(req.body, null, 2));
        try {
            await MemberEntity_1.MemberEntity.delete(userId);
            res.status(200).send({ result: 'OK' });
        }
        catch (error) {
            res.status(200).send({ result: 'Error', message: 'delete error!' + error });
        }
    });
    router.get('/list', async (req, res) => {
        const [dbUser, count] = await MemberEntity_1.MemberEntity.findAndCountBy(req.query);
        console.log('app.locals: => ', req);
        console.log('app.locals: => ', req.params, req.query);
        res.status(200).send({ result: 'OK', data: dbUser, count });
    });
    router.get('/administrator', async (req, res) => {
        const [dbUser, count] = await MemberEntity_1.MemberEntity.findAndCountBy({
            userType: (0, typeorm_1.In)([MemberEntity_1.UserType.admin, MemberEntity_1.UserType.administrator])
        });
        console.log('app.locals: => ', req);
        console.log('app.locals: => ', req.params, req.query);
        res.status(200).send({ result: 'OK', data: dbUser, count });
    });
    router.get('/:userId', (req, res) => {
        console.log('app.locals: => ', app.locals.rooms);
        console.log('app.locals: => ', req.params.userId);
        res.status(200).send('req, res');
    });
    return router;
}
exports.memberRouter = memberRouter;
